#include <iostream>
#include <string>
#include <winsock2.h>
using namespace std;
int main(){
	WORD wVerReq;
	WSADATA wsaData;
	//Using MAKEWORD macro
	wVerReq = MAKEWORD(2,2);
	//
	int wsaErr = WSAStartup(wVerReq,&wsaData);
	if(wsaErr != 0){
		cout << "Ko thay' winsock dll file" << endl;
		return 0;
	}
	//Tao. socket
	// using AF_INET family, TCP socket type and protocol of the AF_INET - IPv4
	SOCKET ser_socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	sockaddr_in ser_socketaddr;

	ser_socketaddr.sin_family = AF_INET;
	ser_socketaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
	ser_socketaddr.sin_port = htons(6874);
	//bind()
	bind(ser_socket,(SOCKADDR*)&ser_socketaddr,sizeof(ser_socketaddr));
	//listen()
	listen(ser_socket,1);
	//accept()
	sockaddr_in cli_socketaddr;
	int cli_socketaddr_len  = sizeof(cli_socketaddr);
	SOCKET cli_sock = accept(ser_socket,(SOCKADDR*)&cli_socketaddr,&cli_socketaddr_len);
	//send() and recv() data
	int sizebuf = 500;
	char buf[501];
	int isok = recv(cli_sock,buf,sizebuf,0);
	if(isok != -1){
		cout <<"client >" << buf << endl;
		string ser_response_msg = "Da nhan. duoc. '"+(string)buf+"'";
		send(cli_sock,ser_response_msg.c_str(),ser_response_msg.size(),0);
	}
	//close()
	closesocket(ser_socket);
	return 0;
}